<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item
        v-for="(item,index) in breadList"
        :key="index"
        :to="{ path: item.path }"
      >{{item.meta.title}}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div>
        <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="120px" status-icon>
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username" />
          </el-form-item>
          <el-form-item label="用户密码" prop="password">
            <el-input v-model="ruleForm.password" type="password" />
          </el-form-item>
          <el-form-item label="用户地址" prop="address">
            <el-input v-model="ruleForm.address" />
          </el-form-item>
          <el-form-item label="电话号码" prop="phone">
            <el-input v-model="ruleForm.phone" />
          </el-form-item>
          <el-form-item label="用户类型" prop="type">
            <el-radio-group v-model="ruleForm.type">
              <el-radio :label="0">管理员</el-radio>
              <el-radio :label="1">普通用户</el-radio>
              <el-radio :label="2">vip用户</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="用户状态" prop="status">
            <el-radio-group v-model="ruleForm.status">
              <el-radio :label="0">停用</el-radio>
              <el-radio :label="1">正常</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <div>
        <el-button type="primary" @click="submitForm(ruleFormRef)">新增</el-button>
          <el-button @click="resetForm(ruleFormRef)">重置表单</el-button>
      </div>
    </div>
  </div>
</template>
<script>
import { useRouter, useRoute } from 'vue-router'
import { ref, reactive, watch } from "vue";
import request from "../../utils/request";
import { ElNotification } from "element-plus";
export default {
  setup() {
    let breadList = [] // 路由集合
    const route = useRoute()
    const getBreadcrumb=(url)=> {
      let matched = url.matched;
      //如果不是首页
      if ("Home"==matched[0].name) {
        matched[0].meta =  { title: "系统首页" } 
      }
      breadList = matched;
    }
    watch(route, (newValue, oldValue) => {
      // console.log(222222,newValue)
      getBreadcrumb(newValue)
    },{ deep: true, immediate: true });
    
    const resetForm = (formEl) => {
      if (!formEl) return;
      formEl.resetFields();
    };
    const submitForm = async (formEl) => {
      if (!formEl) return;
      await formEl.validate((valid, fields) => {
        if (valid) {
          // console.log("提交更新", fields);
          request
            .request({
              url: "/user/add",
              method: "post",
              data: {
                username: ruleForm.username,
                password: ruleForm.password,
                phone: ruleForm.phone,
                address: ruleForm.address,
                type: ruleForm.type,
                status: ruleForm.status,
              },
            })
            .then((res) => {
              if (res.data.code == 200) {
                ElNotification({
                  title: "新增成功",
                  message: "成功更新用户信息",
                });
              } else {
                ElNotification({
                  title: "新增失败",
                  message: "请联系管理员检查失败原因",
                });
              }
            })
            .catch((e) => {
              ElNotification({
                title: "新增失败",
                message: "请联系管理员检查失败原因",
              });
            });
        } else {
          ElNotification({
            title: "校验失败",
            message: "请重新填写信息",
          });
        }
      });
    };
    const ruleForm = reactive({
      userId: "",
      username: "",
      password: "",
      email: "",
      phone: "",
      type: 0,
      status: 1,
    });
    const rules = reactive({
      username: [
        {
          required: true,
          message: "请输入用户名",
          trigger: "blur",
        },
      ],
      password: [
        { required: true, message: "请输入密码", trigger: "blur" },
        { min: 3, max: 10, message: "密码位数只能在3~10之间", trigger: "blur" },
      ],
      address: [
        {
          required: true,
          message: "请输入地址",
          trigger: "blur",
        },
      ],
      phone: [
        {required: true,message: "请输入手机号",trigger: "blur"},
        { pattern: /^1[3456789]\d{9}$/, message: '手机号码格式不正确', trigger: 'blur' }
      ]
    });
    const ruleFormRef = ref();
    const centerDialogVisible = ref(false);
    return {
      breadList,
      resetForm,
      ruleFormRef,
      submitForm,
      ruleForm,
      rules,
      centerDialogVisible,
      
    };
  },
};
</script>
<style lang="scss" scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}

.table {
  width: 100%;
  font-size: 14px;
}

.red {
  color: #ff0000;
}

.mr10 {
  margin-right: 10px;
}

.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
